<template>
  <div>
    <el-dialog
      title="修改用户"
      :visible.sync="IsEditUser"
      width="50%"
      :before-close="handleClose"
      @close="addDialdgClose"
    >
      <!-- 内容区域 -->
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="80px">
        <el-form-item label="编号" prop="UserCode">
          <el-input v-model="editForm.UserCode"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="UserName">
          <el-input v-model="editForm.UserName"></el-input>
        </el-form-item>
        <el-form-item label="真实姓名" prop="RealName">
          <el-input v-model="editForm.RealName"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="UserPwd">
          <el-input type="password" v-model="editForm.UserPwd"></el-input>
        </el-form-item>
        <!-- <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>-->
      </el-form>

      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="IsEditUser = false">取 消</el-button>
        <el-button type="primary" @click="editUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    IsEditUser: {
      type: Boolean,
      default: false
    },
    userdata: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      editForm: {
        ID: 0,
        UserCode: "",
        UserName: "",
        RealName: "",
        UserPwd: ""
      },
      //添加表单的验证规则对象
      editFormRules: {
        UserCode: [
          { required: true, message: "请输入编号", trigger: "blur" },
          { min: 3, max: 11, message: "长度在3到11个字符之间", trigger: "blur" }
        ],
        UserName: [
          { required: true, message: "请输入用户名称", trigger: "blur" }
        ],
        RealName: [
          { required: true, message: "请输入真实姓名", trigger: "blur" }
        ],
        UserPwd: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    //监听关闭添加用户的对话框事件
    addDialdgClose() {
      this.$refs.editFormRef.resetFields();
    },
    //获取用户数据
    getUser(data) {
      if (data.IsShow == true) {
        this.$get("/api/User/GetUser", {
          id: this.userdata.id
        })
          .then(res => {
            if (res.code == 0) {
              this.editForm.ID = this.userdata.id;
              this.editForm.UserCode = res.data.userCode;
              this.editForm.UserName = res.data.userName;
              this.editForm.RealName = res.data.realName;
              this.editForm.UserPwd = res.data.userPwd;
            } else {
              console.log(res.msg);
            }
          })
          .catch(err => {
            console.log(err);
          });
      }
    },
    //修改用户数据
    editUser() {
      this.$refs.editFormRef.validate(valid => {
        if (!valid) {
          return;
        }
        this.$get("/api/User/EditUser", this.editForm)
          .then(res => {
            if (res.code == 0) {
              this.$alert(res.msg, "提示");
              this.$emit("shuaxdata");
              this.IsEditUser = false;
            } else {
              this.$alert(res.msg, "提示");
            }
          })
          .catch(err => {
            console.log(err);
          });
      });
    }
  },
  watch: {
    IsEditUser: function(newVal) {
      let data = {
        IsShow: newVal
      };
      this.getUser(data); //获取用户数据
      this.$emit("IsShowEditUser", data);
    }
  }
};
</script>